//
//  FindViewController.m
//  PetAPP
//
//  Created by dllo on 16/1/29.
//  Copyright © 2016年 Yuming. All rights reserved.
//

#import "FindViewController.h"
#import "CellOfFind.h"
#import "NetworkingHandle.h"
#import "ModelOfFind.h"
#import "FindOfCustomView.h"
#import "SearchViewController.h"
#import <DKNightVersion.h>

#define WIDTH self.view.frame.size.width

@interface FindViewController () <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) UITableView *tableView;

@property (nonatomic, retain) NSMutableArray *dataArr;



@end

@implementation FindViewController

- (void)dealloc {
    [_dataArr release];
    [_tableView release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.dk_backgroundColorPicker = DKColorWithColors([UIColor whiteColor], [UIColor colorWithRed:0.2952 green:0.2952 blue:0.2952 alpha:1.0]);
    [self createTableView];
    [self handleData];
    

}

- (void)viewWillAppear:(BOOL)animated {

    [super viewWillAppear:animated];
    
    

}

- (void)handleData {
    
    [NetworkingHandle handleWorkData:[self.URLDic objectForKey:@"url"] AndBody:[self.URLDic objectForKey:@"body"] value:[self.URLDic objectForKey:@"value"] handleBlock:^(id result) {
        
        NSArray *tempArr = [result objectForKey:@"recarray"];
        self.dataArr = [NSMutableArray array];
        
        for (NSDictionary *dic in tempArr) {
            ModelOfFind *model = [[ModelOfFind alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.dataArr addObject:model];
            [model release];
        }
        [self.tableView reloadData];
    }];
    


}

- (void)createTableView {
    self.tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
    [self.view addSubview:_tableView];
    [_tableView release];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    [self.tableView registerClass:[CellOfFind class] forCellReuseIdentifier:@"poolOfCell"];
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"poolOfMore"];
    [self createTableViewOfHeaderView];
}

- (void)createTableViewOfHeaderView {
    
    
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 200)];
    view.dk_backgroundColorPicker = DKColorWithColors([UIColor whiteColor], [UIColor colorWithRed:0.2952 green:0.2952 blue:0.2952 alpha:1.0]);
    /** 创建搜索 */
    UILabel *labelOfSearch = [[UILabel alloc] initWithFrame:CGRectMake(15, 10, WIDTH - 30, 30)];
    [view addSubview: labelOfSearch];
    [labelOfSearch release];
    labelOfSearch.text = @"  搜索";
    labelOfSearch.dk_textColorPicker = DKColorWithColors([UIColor grayColor], [UIColor blackColor]);
    labelOfSearch.backgroundColor = [UIColor colorWithRed:0.9625 green:0.9625 blue:0.9625 alpha:1.0];
    labelOfSearch.dk_backgroundColorPicker = DKColorWithColors([UIColor colorWithRed:0.9625 green:0.9625 blue:0.9625 alpha:1.0], [UIColor whiteColor]);
    labelOfSearch.font = [UIFont systemFontOfSize:15];
    labelOfSearch.layer.cornerRadius = 7;
    labelOfSearch.layer.masksToBounds = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSearchWihtTap:)];
    labelOfSearch.userInteractionEnabled = YES;
    [labelOfSearch addGestureRecognizer:tap];
    [tap release];
    
    
    /** 创建灰色的线 */
    UIView *garyLine = [[UIView alloc] initWithFrame:CGRectMake(0, 49, WIDTH, 1)];
    garyLine.layer.borderWidth = 1;
    garyLine.layer.borderColor = [UIColor colorWithRed:0.8916 green:0.8916 blue:0.8916 alpha:1.0].CGColor;
    [view addSubview:garyLine];
    
    
    UIImageView *imageViewOfPic = [[UIImageView alloc] init];
    [view addSubview:imageViewOfPic];
    [imageViewOfPic release];
    imageViewOfPic.dk_imagePicker= DKImageWithNames(@"tubiaozutu", @"tubiaozutuNight");
    imageViewOfPic.frame = CGRectMake(0, 50, WIDTH, 150);
    imageViewOfPic.userInteractionEnabled = YES;
    
    for (int i = 0 ; i < 3; i++) {
        FindOfCustomView *customView = [[FindOfCustomView alloc] initWithFrame:CGRectMake(i * (WIDTH / 3), 0, WIDTH / 3, 150)];
        [imageViewOfPic addSubview:customView];
        [customView release];
        customView.tag = i + 10;
        
        customView.block = ^(NSInteger tag) {
            NSLog(@"%ld", tag);
            
        };
        
    }
    
    
    
    

    self.tableView.tableHeaderView = view;

}
#pragma mark - 跳转到搜索页面
- (void)handleSearchWihtTap:(UITapGestureRecognizer *)tap {
    
    SearchViewController *searchVC = [[SearchViewController alloc] init];
    [self.navigationController pushViewController:searchVC animated:YES];
    [searchVC release];

}


#pragma mark - 设置分区标题大小

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 2) {
        return 60;
    }
    return 40;
    
}
#pragma mark - 设置分区标题样式
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if (section != 2) {
        CGRect frameRect = CGRectMake(0, 0, 100, 40);
        
        UILabel *label = [[[UILabel alloc] initWithFrame:frameRect] autorelease];
        if (section == 0) {
            label.text = @"    关注";
        }else {
            label.text = @"    推荐关注";
        }
        
        label.backgroundColor = [UIColor colorWithRed:0.9625 green:0.9625 blue:0.9625 alpha:1.0];
        label.dk_backgroundColorPicker = DKColorWithColors([UIColor colorWithRed:0.9625 green:0.9625 blue:0.9625 alpha:1.0], [UIColor grayColor]);
        
        return label;
    }else {
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 60)];
        view.dk_backgroundColorPicker = DKColorWithColors([UIColor colorWithRed:0.9625 green:0.9625 blue:0.9625 alpha:1.0], [UIColor colorWithRed:0.2952 green:0.2952 blue:0.2952 alpha:1.0]);
        
        
        UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
        button.frame = CGRectMake(0, 10, WIDTH, 40);
        
        [button setTitle:@"+ 更多圈子" forState:UIControlStateNormal];
        button.dk_backgroundColorPicker = DKColorWithColors([UIColor whiteColor], [UIColor grayColor]);
        [button dk_setTitleColorPicker:DKColorWithColors([UIColor blackColor], [UIColor whiteColor]) forState:UIControlStateNormal];
        
        [view addSubview:button];
        [button release];
        
        
        return view;
    }
    
}



#pragma mark - 设置分区
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 3;
}

#pragma mark - 设置分区行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0)
        return 0;
    else if(section == 1)
        return self.dataArr.count;
    else
        return 0;
}

#pragma mark - 返回cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    CellOfFind *cell = [tableView dequeueReusableCellWithIdentifier:@"poolOfCell"];
    if (indexPath.section == 0) {
        cell.flag = NO;
        
        
        return  cell;
        
    }else if (indexPath.section == 2) {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"poolOfMore"];
        return cell;
    
    }else {
        cell.flag = YES;
        
        ModelOfFind *model = [self.dataArr objectAtIndex:indexPath.row];
        
        [cell handleDataWithModel:model];
        
        
        return  cell;
    
    }
}

#pragma mark - 设置每行的行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    return 70;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
